package com.zjxf.controller;

import com.alibaba.fastjson.JSONObject;
import com.zjxf.base.bean.po.Admin;
import com.zjxf.base.bean.po.Store;
import com.zjxf.base.bean.ro.RoStore;
import com.zjxf.base.bean.vo.VoStore;
import com.zjxf.base.common.JSONResponse;
import com.zjxf.base.common.OperationException;
import com.zjxf.base.common.SysConst;
import com.zjxf.controller.common.BaseController;
import com.zjxf.service.StoreService;
import com.zjxf.utils.RadomUP;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 商铺
 *
 * @author ZhangWei
 * @date 2021/3/27/027 15:56
 */
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@Api(tags = "商铺")
@RequestMapping("/store/")
public class StoreController extends BaseController {

    private final StoreService storeService;

    @ApiOperation(value = "添加或修改")
    @PostMapping(value = "add")
    public JSONResponse addOrUpdate(@Valid @RequestBody VoStore.addOrUpdate voStore) throws OperationException {
        Admin admin = (Admin) getUser();
        RadomUP.UP up= storeService.add(voStore, admin);
        return JSONResponse.success(up);
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "update")
    public JSONResponse Update(@Valid @RequestBody VoStore.addOrUpdate voStore) throws OperationException {
        Admin admin = (Admin) getUser();
        storeService.Update(voStore, admin);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "delete")
    public JSONResponse delete(@NotNull(message = "商铺id不能为空") @RequestParam Long id) throws OperationException {
        storeService.delete(id);
        return JSONResponse.success(SysConst.SUCCESS);
    }

    @ApiOperation(value = "分页")
    @GetMapping(value = "page")
    public JSONResponse page(@Valid @RequestBody VoStore.filter filter) {
        Page<Store> storePage = storeService.page(filter);
        JSONObject jsonData = storeService.conversion(storePage);
        return JSONResponse.success(jsonData);
    }

    @ApiOperation(value = "回显")
    @GetMapping(value = "getEntity")
    public JSONResponse getEntity(@NotNull(message = "商铺id不能为空") @RequestParam Long id) throws OperationException {
        Store store = storeService.getEntity(id);
        RoStore.entity roStore= storeService.getEntityRo(store);
        return JSONResponse.success(roStore);
    }



}
